package com.xiaoq.distribution.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGeneratorUtils;
import com.xiaoq.distribution.entity.AgreementEntity;
import com.xiaoq.distribution.repository.IAgreementRepository;
import com.xiaoq.vo.PagingVo;

@Repository
public class AgreementRepositoryImpl implements IAgreementRepository {
	
	@Autowired
	private Datastore datastore;

	@Override
	public boolean save(AgreementEntity agree) {
		datastore.save(agree);
		return true;
	}

	@Override
	public AgreementEntity findById(String id) {
		AgreementEntity agreementEntity = new AgreementEntity();
		Query<AgreementEntity> agreeQuery = datastore.find(AgreementEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(id);
		agreementEntity = agreeQuery.get();
		return agreementEntity;
	}

	@Override
	public List<AgreementEntity> findByShopId(String shopId, PagingVo pvo) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Long total = 0L;
		List<AgreementEntity> agreeList = new ArrayList<>();
		Query<AgreementEntity> agreeQuery = datastore.find(AgreementEntity.class).field("shopId").equal(shopId);
		total = agreeQuery.count();
		agreeList = agreeQuery.offset(pn).limit(ps).asList();
		pvo.setTc(total);
		return agreeList;
	}

}
